package org.micro.rbac;

import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.core.util.StrUtil;
import com.zaxxer.hikari.HikariDataSource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.micro.rbac.util.AppUtil;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * 入口
 *
 * @author hongsir
 * @since 2024/2/22 0022
 **/
@Slf4j
@SpringBootApplication
@EnableTransactionManagement
@MapperScan("org.micro.rbac.dao.mapper")
public class Main  {

    @SneakyThrows
    public static void main(String[] args) {
        ConfigurableApplicationContext application = SpringApplication.run(Main.class, args);
        Environment env = application.getEnvironment();

        log.info("最大内存:{}", AppUtil.calcSize(RuntimeUtil.getMaxMemory()));
        log.info("总内存数:{}", AppUtil.calcSize(RuntimeUtil.getTotalMemory()));
        log.info("可用内存:{}", AppUtil.calcSize(RuntimeUtil.getUsableMemory()));
        log.info("剩余内存:{}", AppUtil.calcSize(RuntimeUtil.getFreeMemory()));

        log.info("接口文档:http://localhost:{}/doc.html\t帐号:{} 密码:{}",
                env.getProperty("server.port"),
                env.getProperty("knife4j.basic.username"),
                env.getProperty("knife4j.basic.password")
        );
        if (application.getBean(DataSource.class) instanceof HikariDataSource dataSource) {
            log.info("H2-WEB:http://localhost:{}{} \t 管理员密码:{}",
                    env.getProperty("server.port"),
                    env.getProperty("spring.h2.console.path"),
                    env.getProperty("spring.h2.console.settings.web-admin-password")
            );
            log.info("H2-链接:{}", dataSource.getJdbcUrl());
            log.info("H2-帐号:{} 密码:{}", dataSource.getUsername(),
                    StrUtil.blankToDefault(dataSource.getPassword(), "无")
            );
        }
    }

}
